import { aiStudyApi } from '@/api';
import { Cascader, Form, message, Select } from 'antd';
import { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
const rules = (text: string) => {
  return [{ required: true, message: `${text}` }];
};
export type SelContRef = {
  onSubmit: () => Promise<any>;
};
let cascKey = 0;
type FormValue = {
  sourtype?: number;
  class?: string;
};
const SelAiModal = forwardRef<SelContRef, any>((props, ref) => {
  console.log(props, '打印看看接收的参数');
  // if (!props.parentClassName)
  const [form] = Form.useForm<FormValue>();
  const [eventOptions, setEventOptions] = useState<any[]>([]);
  const [showName, setShowName] = useState<any[]>([]);
  const sourceList = [
    {
      value: 1,
      label: '课件包',
    },
    // {
    //   value: 2,
    //   label: '试卷',
    // },
  ];
  const getEventsList = () => {
    aiStudyApi.getAiContList({}).then((res: any) => {
      const resdata = res.data.list;
      // setEventList(resdata);
      // console.log(resdata, res, '打印看看');
      const newArr = resdata.map((el: any) => {
        return {
          label: el.className,
          value: el.guid,
          children: el.lowerListVo.map((item: any) => {
            return { label: item.className, value: item.guid, disabled: item.canChoose == 0 };
          }),
          disabled: el.lowerListVo.length < 1,
        };
      });
      // eventOptions = [...newArr];
      setEventOptions(newArr);
      // keytwo = keytwo + 1;
      // setkeytwo(keytwo);
    });

    // setEventList(resdata);
    // const newArr = resdata.map((el) => {
    //   return {
    //     label: el.groupName,
    //     value: el.groupId,
    //     children: el.safetyFactorDetailVoList.map((item) => {
    //       return { label: item.factorName, value: item.factorId };
    //     }),
    //   };
    // });
    // eventOptions = [...newArr];
    // setEventOptions(newArr);
    // keytwo = keytwo + 1;
    // setkeytwo(keytwo);
  };
  const onSubmit = async () => {
    let errorClass = false;
    eventOptions.forEach((el: any) => {
      el.children.forEach((item: any) => {
        if (item.value == props.record.classId && item.disabled) errorClass = true;
      });
    });
    if (errorClass) {
      message.error('不能选择没课件包的分类');
      return false;
    }
    if (!props.record.classId) return message.error('请选择分类');
    else return Promise.resolve({ ...(await form.validateFields()), ...props });
  };
  useImperativeHandle(ref, () => ({
    onSubmit,
  }));
  useEffect(() => {
    form.setFieldsValue({ sourtype: props.record.type });
    if (props.record.parentClassName && props.record.className) {
      setShowName([props.record.parentClassName, props.record.className]);
      cascKey = cascKey + 1;
    }

    getEventsList();
  }, []);
  return (
    <div style={{ padding: '16px 16px 0' }}>
      <Form layout="horizontal" form={form}>
        <Form.Item name="sourtype" label="资源" rules={rules('请选择资源')}>
          <Select placeholder="请选择" options={sourceList} />
        </Form.Item>
        <Form.Item name="class" label="">
          <div style={{ display: 'inline-block', marginRight: 3 }}>
            <span style={{ color: '#ff4d4f', marginRight: 3 }}>*</span>
            <span>分类：</span>
          </div>
          <Cascader
            style={{ width: 250 }}
            key={cascKey}
            placeholder="请选择分类"
            options={eventOptions}
            onChange={(e, value) => {
              props.record.parentClassName = value[0].label;
              props.record.className = value[1].label;
              props.record.classId = value[1].value;
            }}
            maxTagCount="responsive"
            defaultValue={showName}
          />
        </Form.Item>
      </Form>
    </div>
  );
});
export default SelAiModal;
